package com;

import java.util.EnumSet;

import javax.servlet.DispatcherType;

import org.eclipse.jetty.server.ConnectionFactory;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import com.pre.CorsHandler;
import com.pre.Filter1;
import com.pre.Filter2;
import com.servlet.MyServlet;
import com.sun.jersey.spi.container.servlet.ServletContainer;

import hyl.core.MyFun;
import hyl.core.conf.MyConst;


public class App {
	private static String HTTP_IP = "0.0.0.0";
	private static int HTTP_PORT = 80;
	private static int HTTPS_PORT = 8443;
	private static String CONTEXT = "/";
	public static void main(String[] args) throws Exception {

		MyConst sysdict=MyConst.getInstance();

		HTTP_IP = sysdict.get("IP");
		HTTP_PORT = MyFun.str2int(sysdict.get("HTTPPORT"));
		HTTPS_PORT = MyFun.str2int(sysdict.get("SSLPORT"));
		CONTEXT = sysdict.get("CONTEXT");
		Server server = new Server();

		HttpConfiguration httpConfig = new HttpConfiguration();
		if (HTTPS_PORT > 0) {
			httpConfig.setSecureScheme("https");
			httpConfig.setSecurePort(HTTPS_PORT);
		}
		httpConfig.setOutputBufferSize(32768);
		httpConfig.setRequestHeaderSize(8192);
		httpConfig.setResponseHeaderSize(8192);
		httpConfig.setSendServerVersion(true);
		httpConfig.setSendDateHeader(false);
		httpConfig.setHeaderCacheSize(512);
		ConnectionFactory connectionFactory = new HttpConnectionFactory(httpConfig);
		ServerConnector connector = new ServerConnector(server, connectionFactory);
		connector.setPort(HTTP_PORT);
		connector.setSoLingerTime(-1);
		// 连接线程最大空闲时间
		connector.setIdleTimeout(30000);
		server.addConnector(connector);
		

		ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
		context.setContextPath(CONTEXT);
		context.addServlet(全局句柄(), "/*"); // 并行处理
		context.addServlet(new ServletHolder(new MyServlet()), "/h");
		context.addFilter(Filter1.class, "/*", EnumSet.of(DispatcherType.REQUEST));// 串行处理
		context.addFilter(Filter2.class, "/*", EnumSet.of(DispatcherType.REQUEST));

		CorsHandler corsHandler = new CorsHandler();// 跨域处理
		HandlerCollection hc = new HandlerCollection();
		hc.setHandlers(new Handler[] { corsHandler, context }); // 如果有多个handle 在这里添加

		server.setHandler(hc);
		// context.setHandler(单个句柄());//两次setHandler只有后面这次有用
		server.start();
		System.out.println("开启...端口= " + HTTP_PORT);
	}

	public static ServletHolder 全局句柄() {
		ServletHolder servlet = new ServletHolder(ServletContainer.class);
		// 设置初始化参数
		servlet.setInitParameter("com.sun.jersey.config.property.resourceConfigClass",
				"com.sun.jersey.api.core.PackagesResourceConfig");
		servlet.setInitParameter("com.sun.jersey.config.property.packages", "com.control");
		servlet.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true"); // 自动将对象映射成json返回

		return servlet;
	}
}
